/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

/* eslint-disable import/no-extraneous-dependencies */
import React, { useState, useRef } from 'react';
import { useSelector } from 'dva';
import router from 'umi/router';
import Prompt from 'umi/prompt';
import ConfirmModal from './ConfirmModal';

/**
 * 共配授权路由拦截
 *  */
const GpAuthPrompt = () => {
  const { setting = {}, user = {} } = useSelector(state => state);
  const [visible, setVisible] = useState(false);
  const nextRouter = useRef('');

  const { options: { key } = {} } = setting;
  const { currentUser: { disclaimer_status } = {} } = user;

  const onPrompt = location => {
    if (key == 'yz') {
      if (location.pathname.indexOf('Allocation') >= 0 && disclaimer_status == 0) {
        nextRouter.current = location.pathname;
        setVisible(true);
        return false;
      }
      return true;
    }
    return true;
  };

  const onOk = () => {
    router.push(nextRouter.current);
    setVisible(false);
  };

  return (
    <>
      <Prompt message={onPrompt} />
      <ConfirmModal
        visible={visible}
        onOk={onOk}
        onCancel={() => {
          setVisible(false);
        }}
      />
    </>
  );
};

export default GpAuthPrompt;
